#include <REGX52.H>
#include <intrins.h>
#include <string.h>
#include <delay.h>
#include <motor.h>

#define SIZE 12


sfr AUXR = 0x8e;
		

char buffer[SIZE];
  

void InitUart(){

	AUXR  = 0x01;
	SCON  = 0x50;
	TMOD &= 0xF0;
	TMOD |= 0x20;
	
	TH1   = 0xFD;
	TL1   = 0xFD;
	TR1   = 1;
	
	EA    = 1;
	ES    = 1;

}

void SendByte(char data_masg){
	
	SBUF = data_masg;
	while(!TI);
	TI = 0;
}

void Sendstring(char* str){

	while( *str != '\0'){
		
		SendByte(*str);
		str++;
	}
}



void Uart_Handler() interrupt 4
{	
	char tmp = 0;
	static int i = 0;
	
	if(RI)//中断处理函数中，对于接收中断的响应
	{
		RI = 0;//清除接收中断标志位
		tmp = SBUF;//临时变量接收第一位数据
		
		//位置预设，将传进来的相关数据置位为第0位
		if(tmp == 'M'){
			i = 0;
		}
		buffer[i++] = tmp;
			
		
		if(buffer[0] == 'M' && buffer[2] == 'F'){	
			goForward();
			Delay100ms();
			memset(buffer, '\0', SIZE);

		}
		if(buffer[0] == 'M' && buffer[2] == 'B'){	
			goBack();
			Delay100ms();
			memset(buffer, '\0', SIZE);

		}
		if(buffer[0] == 'M' && buffer[2] == 'R'){	
			goRight(); 
			Delay100ms();
			memset(buffer, '\0', SIZE);

		}
		if(buffer[0] == 'M' && buffer[2] == 'L'){	
			goLeft();
			Delay100ms();
			memset(buffer, '\0', SIZE);
	
		}

		if(i == SIZE) 
		{
			i = 0;
			memset(buffer, '\0', SIZE);
		}
	}	
}
	
